;(function($){
  var nameProvince = 'clsProvince';//省|市
  var nameCity = 'clsCity';//地州市
  var nameCounty = 'clsCounty';//区|县
  var nameTowns = 'clsTowns';//乡|镇|社区
  var nameVallage = 'clsVallage';//村|居委会
  var nameGroup = 'clsGroup';//组|街道

  var clsProvince = '.'+nameProvince;
  var clsCity = '.'+nameCity;
  var clsCounty = '.'+nameCounty;
  var clsTowns = '.'+nameTowns;
  var clsVallage = '.'+nameVallage;
  var clsGroup = '.'+nameGroup;

  var arrsProvince = [clsProvince,clsCity,clsCounty,clsTowns,clsVallage,clsGroup];
  var arrsCity = [clsCity,clsCounty,clsTowns,clsVallage,clsGroup];
  var arrsCounty = [clsCounty,clsTowns,clsVallage,clsGroup];
  var arrsTowns = [clsTowns,clsVallage,clsGroup];
  var arrsVallage = [clsVallage,clsGroup];
  var arrsGroup = [clsGroup];
  //动态创建下拉列表,需提供一个div容器的id、请求的url，父级的id，需要生成下拉列表的json数据[含开始的class，如果开始显示的是省级则start=clsProvince；不限制的end的class,若不想显示乡镇及后面的则end=clsTowns，若不想限制级别的那end可以不写]
  window.optsArea = {
    /**无区域权限,start是从那开始显示的class,end是终止请求的class，若不想限制级别的那end可以不写*/
    /**用法：optsArea.initArea(urlQueryArea,'0','#divArea',"{\"clsProvince\":\"选择省|市\",\"clsCity\":\"选择地州市\",\"clsCounty\":\"请选择区|县\",\"clsTowns\":\"选择乡|镇|社区\",\"clsVallage\":\"选择村|居委会\",\"clsGroup\":\"选择组|街道\",\"start\":\"clsProvince\",\"end\":\"clsTowns\"}");*/
    initArea : function(url,pid,containerDom,objs){
      var json = JSON.parse(objs);//转换为json
      var startDom = json['start'] || '';//开始显示的下拉菜单
      var style = json['style'] || '';//添加样式的类，如 style:col-sm-12 form-control
      var node = '';
      if(startDom == nameProvince){
        node = arrsProvince;
      }else if(startDom == nameCity){
        node = arrsCity;
      }else if(startDom == nameCounty){
        node = arrsCounty;
      }else if(startDom == nameTowns){
        node = arrsTowns;
      }else if(startDom == nameVallage){
        node = arrsVallage;
      }else if(startDom == nameGroup){
        node = arrsGroup;
      }
      if(node != ''){
        var html = '';
        var objects = JSON.stringify(objs);
        $.each(node,function(index,item){          
          item = item.substring(1);
          var nextNode  = '';//不带.
          if(item == nameProvince){
            nextNode = nameCity;
          }else if(item == nameCity){
            nextNode = nameCounty;
          }else if(item == nameCounty){
            nextNode = nameTowns;
          }else if(item == nameTowns){
            nextNode = nameVallage;
          }else if(item == nameVallage){
            nextNode = nameGroup;
          }
          html += "<select class='"+item+' '+style+"' style='display:none;cursor:pointer;' onchange='optsArea.onChange(this.value,\""+url+"\",\""+containerDom+"\",\""+nextNode+"\","+objects+")'></select>";
        });
        $(containerDom).html(html);//先创建下拉列表的总个数,下行请求数据填充
        this.getOptsData(url,pid,containerDom,objs,startDom);
      }
    },
    /**有区域权限，用法:optsArea.initUserArea(urlQueryArea,'0','#divArea',"{\"clsProvince\":\"选择省|市\",\"clsCity\":\"选择地州市\",\"clsCounty\":\"请选择区|县\",\"end\":\"clsTowns\",\"parentDom\":\"areaSelectText\"}");*/
    initUserArea : function(url,pid,containerDom,objs){
      var level = sessionStorage.getItem("areaLevel");//此值是在登录成功时存储的
      var provinceId = sessionStorage.getItem("areaProvince");
      var cityId = sessionStorage.getItem("areaCity");
      var json = JSON.parse(objs);//转换为json
      var node = '';
      var startDom = '';
      if(level == 8){//admin或超级管理员,显示1,2,3
        node = arrsProvince;
        startDom = 'clsProvince';
      }else if(level == 1){//显示2,3，是省级或管理员
        pid = provinceId;
        node = arrsCity;
        startDom = 'clsCity';
      }else if(level == 2){//显示3
        pid = cityId;
        node = arrsCounty;
        startDom = 'clsCounty';
      }else if(level == 3 || level < 8){//3或其它值不显示
        var parentDom = json['parentDom'];//移除或隐藏的区域的父容器的dom
        $('.'+parentDom).remove();
      }
      var style = json['style'] || '';//添加样式的类，如 style:col-sm-12 form-control
      if(node != ''){
        var html = '';
        var objects = JSON.stringify(objs);
        $.each(node,function(index,item){
          item = item.substring(1);
          var nextNode  = '';//不带.
          if(item == nameProvince){
            nextNode = nameCity;
          }else if(item == nameCity){
            nextNode = nameCounty;
          }else if(item == nameCounty){
            nextNode = nameTowns;
          }else if(item == nameTowns){
            nextNode = nameVallage;
          }else if(item == nameVallage){
            nextNode = nameGroup;
          }
          html += "<select class='"+item+' '+style+"' style='display:none;cursor:pointer;' onchange='optsArea.onChange(this.value,\""+url+"\",\""+containerDom+"\",\""+nextNode+"\","+objects+")'></select>";
        });
        $(containerDom).html(html);//先创建下拉列表的总个数,下行请求数据填充
        this.getOptsData(url,pid,containerDom,objs,startDom);
      }
    },
    //处理是否隐藏后面的下拉列表
    onChange : function(value,url,containerDom,childDom,objs){
      if(childDom == nameCity){
        this.handerHide(containerDom,arrsCity);
      }else if(childDom == nameCounty){
        this.handerHide(containerDom,arrsCounty);
      }else if(childDom == nameTowns){
        this.handerHide(containerDom,arrsTowns);
      }else if(childDom == nameVallage){
        this.handerHide(containerDom,arrsVallage);
      }
      if(value){
        this.getOptsData(url,value,containerDom,objs,childDom);
      }
    },
    handerHide : function(containerDom,arrsDisplay){
      $.each(arrsDisplay,function(index,item){
        $(containerDom +' '+item).val('');//全部清空
        $(containerDom +' '+item).css({"display":"none"});//除了当前的dom外其余的全部隐藏
        $(containerDom +' '+item +' option').remove();//移除dom
      });
    },
    getOptsData : function(url,pid,containerDom,objs,showDom){
      var json = JSON.parse(objs);
      var end = json['end'] || '';//隐藏指定的下拉列表及其后面的下拉列表
      if(showDom == end){
        return;
      }
      layerFn.queryGetHintResult(url,{pid : pid},function(data){
        var exist = AppKey.code.code200 == data.code;
        var showText = exist ? json[showDom] : data.msg;
        var html = '<option value="">'+showText+'</option>';
        if(exist){
          $.each(data.data,function(index,item){
            html += "<option value='"+item.value+"'>"+item.label+"</option>";
          });
        }
        $(containerDom + ' .' + showDom).html(html);
        $(containerDom + ' .' + showDom).css({"display":"inline"});
      });
    }
  };
})(jQuery);